package com.cos.openapi.services.impl;

import com.cos.openapi.dao.RoleMapper;
import com.cos.openapi.entity.Menu;
import com.cos.openapi.entity.Role;
import com.cos.openapi.pojo.JsonResult;
import com.cos.openapi.pojo.TableResult;
import com.cos.openapi.services.RoleServices;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: COS
 * @time: 2021/2/1 15:15
 * @description:
 */
@Service
public class RoleServicesImpl implements RoleServices {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insertSelective(Role record) {
        return 0;
    }

    @Override
    public Role selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {
        return 0;
    }

    @Override
    public List<Role> getRoles() {
        return roleMapper.getRoles();
    }

    @Override
    public TableResult table(Integer page, Integer limit, String name) {

        PageHelper.startPage(page, limit);
        List<Role> roleList = roleMapper.table(name);
        PageInfo<Role> info = new PageInfo<>(roleList);

        return TableResult.getResultBySuccess(info.getTotal(), info.getList());
    }

    @Override
    public JsonResult updateStatus(Integer id, Integer status) {
        if (id > 0 && status != null) {
            if (roleMapper.updateStatus(id, status) > 0) {
                return JsonResult.getJsonResultBySuccess("成啦");
            }
        }
        return JsonResult.getJsonResultByFail("失败啦哈哈");
    }

    @Override
    public List<Integer> roleMenu(Integer roleId) {
        if (roleId > 0) {
            return roleMapper.roleMenu(roleId);
        }
        return null;
    }

    @Override
    public JsonResult assignMenu(Integer roleId, Integer[] menuIds) {
        try {
            roleMapper.delRoleMenuByRoleId(roleId);
            if (menuIds != null && menuIds.length > 0) {
                roleMapper.addRoleMenu(roleId, menuIds);
            }
            return JsonResult.getJsonResultBySuccess("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResult.getJsonResultByFail("修改失败");
    }

    /**
     * @author: COS
     * @return:
     * @time: 2021/2/2 9:43
     * @description: 要求对应新增或者修改
     */
    @Override
    public JsonResult updateRole(Role role) {
        //role.getId() 主键不为空格为修改
        int i = 0;
        if (role.getId() != null) {
            i = roleMapper.updateByPrimaryKeySelective(role);
        } else {
            i = roleMapper.insertSelective(role);
        }
        if (i > 0) {
            return JsonResult.getJsonResultBySuccess("成功");
        }
        return JsonResult.getJsonResultByFail("失败");
    }
}
